﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Product;

namespace BrokerBase.Lib.Policy
{
    public class SchemeActing:ISchemeActing
    {
        private ISchemePeriodActing[] spas;
        private IPolicyActing[] pas;
        private IScheme scheme;
        private int maxPeriod;
        private IDictionary<IAuxProperty, object> auxConfigs;
        //private IList<string> states;

        public SchemeActing(IScheme scheme, int defaultMaxAge)
        {
            this.scheme = scheme;

            pas = new IPolicyActing[scheme.Policies.Count];

            maxPeriod = scheme.GetPeriodCount(defaultMaxAge);

            RunTimeProperties = new Dictionary<string, object>();

            for (int i = 0; i < scheme.Policies.Count; i++)
            {
                pas[i] = new PolicyActing(this, scheme.Policies[i]);
            }

            spas = new ISchemePeriodActing[maxPeriod];

            int age = scheme.InsurantAge;

            for(int j=0;j<spas.Count();j++)
            {
                spas[j] = new SchemePeriodActing(this,j,age+j);

            }

        }

        public IScheme Scheme 
        {
            get { return scheme; }
        }

        public ISchemePeriodActing[] PeriodActings 
        {
            get { return spas; }
        }

        public IPolicyActing[] PolicyActings
        {
            get { return pas; }
        }

        public int MaxPeriod { get { return maxPeriod; } }


        public IDictionary<IAuxProperty, object> AuxConfigs 
        {
            get
            {
                if (auxConfigs == null)
                {
                    auxConfigs = new Dictionary<IAuxProperty, object>();

                    foreach (IPolicyActing pa in PolicyActings)
                    {
                        foreach (KeyValuePair<IAuxProperty, object> auxc in pa.AuxConfigs)
                        {
                            if (auxConfigs.FirstOrDefault(p => p.Key.Name == auxc.Key.Name && p.Key.DataType == auxc.Key.DataType).Key==null)
                            {
                                auxConfigs.Add(auxc);
                            }
                        }
                    }
                }

                return auxConfigs;
            }
        }


        public object GetAuxConfigValue(string name)
        {
            KeyValuePair<IAuxProperty, object> auxc = AuxConfigs.FirstOrDefault(p => p.Key.Name == name);

            object result=null;

            result=auxc.Key!=null?auxc.Value:null;

            return result;

        }

        public IDictionary<string, object> RunTimeProperties { get; set; }

        public void Calc()
        {
            foreach (var spa in spas)
            {
                spa.Calc();
            }
        }

        public int BeginYear { get; set; }
    }
}
